package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.oomall.core.model.VoObject;
import cn.edu.xmu.oomall.core.util.JacksonUtil;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.customer.microservice.CouponActivityService;
import cn.edu.xmu.oomall.customer.microservice.GoodsService;
import cn.edu.xmu.oomall.customer.microservice.vo.CouponActivityRetVo;
import cn.edu.xmu.oomall.customer.microservice.vo.CouponActivityVoInfo;
import cn.edu.xmu.oomall.customer.microservice.vo.ProductRetVo;
import cn.edu.xmu.oomall.customer.microservice.vo.ProductVoInfo;
import cn.edu.xmu.oomall.customer.model.vo.*;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.dao.CartDao;
import cn.edu.xmu.oomall.customer.model.po.ShoppingCartPo;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
/**
 * @author Haotian Cheng
 * @date 2021-12-14 12:35:06
 * @modifiedBy Jiazhe Yuan
 * @date 2021-12-14 12:35:30
 * @modifiedBy Jiazhe Yuan
 * @time 2021-12-23 11:33:24
 * @info 对接oomall-additional
 */
public class CartService {

    @Autowired
    private CartDao cartDao;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CouponActivityService couponActivityService;


    @Transactional(rollbackFor = Exception.class)
    public ReturnObject<PageInfo<VoObject>> getCarts(Long customerId, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        PageInfo<ShoppingCartPo> shoppingCartPos = cartDao.getCarts(customerId);

        List<VoObject> carts = new ArrayList<>();

        try {
            for (var tmp : shoppingCartPos.getList()) {
                CartRetVo cartRetVo = Common.cloneVo(tmp, CartRetVo.class);
                var productDetails = goodsService.getProductDetails(tmp.getProductId());
                if (!productDetails.getErrno().equals(ReturnNo.OK.getCode())) {
                    return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, productDetails.getErrmsg());
                }
                System.out.println(JacksonUtil.toJson(productDetails));

                ProductRetVo productVoInfo = (ProductRetVo) productDetails.getData();

                InternalReturnObject<PageVo<CouponActivityRetVo>> couponActivityByProductId = couponActivityService.listCouponActivitiesByProductId(tmp.getProductId(), page, pageSize);
                if (!couponActivityByProductId.getErrno().equals(ReturnNo.OK.getCode())) {
                    return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, couponActivityByProductId.getErrmsg());
                }

                List<SimpleCouponActivityRetVo> list = new ArrayList<>();
                for (var tmp1 : couponActivityByProductId.getData().getList()) {
                    list.add(Common.cloneVo(tmp1, SimpleCouponActivityRetVo.class));
                }

                cartRetVo.setCouponActivity(list);
                cartRetVo.setProduct(Common.cloneVo(productVoInfo, SimpleProductRetVo.class));
                carts.add(cartRetVo);
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }

        PageInfo<VoObject> returnObject = new PageInfo<>(carts);
        returnObject.setPages(shoppingCartPos.getPages());
        returnObject.setPageSize(shoppingCartPos.getPageSize());
        returnObject.setPageNum(shoppingCartPos.getPageNum());
        returnObject.setTotal(shoppingCartPos.getTotal());
        return new ReturnObject<>(returnObject);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject postCarts(Long customerId, String customerName, SimpleCartItemVo simpleCartItemVo) {
        var product = goodsService.getProductDetails(simpleCartItemVo.getProductId());
        System.out.println(JacksonUtil.toJson(product));
        if (!product.getErrno().equals(ReturnNo.OK.getCode())) {
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, product.getErrmsg());
        }
        ProductRetVo retPro = (ProductRetVo) product.getData();
        ReturnObject ret = cartDao.createNewCartItem(customerId, customerName, retPro, simpleCartItemVo.getQuantity());
        ShoppingCartPo shoppingCartPo = (ShoppingCartPo) ret.getData();
        SimpleCartRetVo simpleCartRetVo = Common.cloneVo(shoppingCartPo, SimpleCartRetVo.class);
        return new ReturnObject<>(simpleCartRetVo);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject deleteCarts(Long customerId) {
        return cartDao.deleteCarts(customerId);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject updateProductInCartsById(Long customerId, String customerName, Long id, SimpleCartItemVo simpleCartItemVo) {
        return cartDao.updateProductInCartsById(customerId, customerName, id, simpleCartItemVo);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject deleteProductInCartsById(Long customerId, Long id) {
        return cartDao.deleteCartsById(customerId, id);
    }


}
